<?php
/******************************************************* *   Copyright (C) 2007  http://p3net.net    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License along    with this program; if not, write to the Free Software Foundation, Inc.,    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    
    @id: $Id: gallery.php 8 2007-07-27 20:56:25Z p3net.tech $
*********************************************************/
class gallery
{
	function view($id)
	{
		if($id == 0)
		{
			$error->general('Must be logged in!', 'Tried to access gallery as owner while unauthenticated');
		}
		else
		{
			if($session->is_friend($id))
			{
				$_query="SELECT `id` FROM `images` WHERE `owner`='" . $id . "'";
				$_query=$db->query($_query);
				$_query=$db->fetch_array($_query);
				$gallery =& new template('gallery.tpl');
				$gallery->set('gallery', $_query);
			}
		}
	}
	function drill($img_id, $owner)
	{
		if(empty($img_id))
		{
			$error->general('An image must be specified', 'Tried to access drill without specifying image id');
		}
		else
		{
			if($session->is_friend($owner))
			{
				$_query = "SELECT * FROM `images` WHERE `id`='" . $img_id . "'";
				$img = $db->query($_query);
				
				$_query = "SELECT * FROM `images_comments` WHERE `image`='" . $img_id . "'";
				$img_com = $db->query($_query);
				
				foreach($db->fetch_array($img_com) as $key => value)
				{
					foreach($value as $key_name => $key_value)
					{
						if($key_value == 'author')
						{
							$key_value = $session->get_username($key_value);
						}
						$com[$key_name] = $key_value;
					}
				}
				$img=$db->fetch_array($img);
				if(isset($user->data['user_id'] && $user->data['user_id'] != $img['owner'])
				{
					$img['views']++;
					$_query="UPDATE `images` SET `views`='" . $views . "' WHERE `id`='" . $img_id . "'";
					$db->query($_query);
				}
				$drill =& new template('drill.tpl');
				$drill->set('id', $img_id);
				$drill->set('comments', $com);
				$drill->set('views', $img['views']);
				$drill->set('desc', $img['desc']);
				$drill->set('name', $img['name']);
			}
		}
	}
	function comment($id, $owner)
	{
		if($session->is_friend($owner))
		{
			$form =& new template('forms/gallery_comment.tpl');
		}
	}
	function comment_process()
	{
		$img_id=$_POST["id"];
		$session->add_image_comment($img_id);
	}
}
$gallery =& new gallery;
$mode = empty($_GET["mode"]) ? 'view' : $_GET["mode"];
$id = empty($_GET["id"]) ? ($session->logged_in() ? $user->data['user_id'] : 0) : mysql_real_escape_string($_GET["id"]);
switch $mode
{
	case 'view':
		$gallery->view($id);
		break;
	case 'drill':
		$gallery->drill(mysql_real_escape_string($_GET["img"]), $id);
		break;
	case 'comment':
		$gallery->comment($id, $owner_id);
		break;
	case 'process':
		$gallery->comment_process();
		break;
}
?>